using System;
using Foundry.Engine;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

namespace Foundry.Graphics
{
	public class BoxRenderer : Modifier
	{
		DefaultShader shader;
		VertexArray<PositionNormalTexture> va;
		IndexBuffer<ushort> ib;
		
		public BoxRenderer ()
		{
		}
		
		public void Initialize()
		{
			shader = new DefaultShader(PositionNormalTexture.Descriptor);
			shader.CompileAndLink();
			shader.MakeCurrent();
			//shader.Projection.Value = Scene.Owner.CurrentCamera.Projection;
			//shader.Model.Value = Parent.AbsoluteTransform;
			//shader.NormalMatrix.Value = Matrix4.Identity;
			//shader.View.Value = Scene.Owner.CurrentCamera.AbsoluteTransform;
			//shader.ModelViewProjection.Value = shader.Projection.Value;
			
			va = new VertexArray<PositionNormalTexture>();
			va.Buffer.Allocate(8);
			
			ib = new IndexBuffer<ushort>();
			ib.Allocate(12);
			float s = .5f;
			
			
			// Back
			va.Buffer[0] = new PositionNormalTexture()
			{
				Pos = new Vector3(-s, -s, s),
				Normal = new Vector3(0, 0, -1),
				TexCoord = new Vector2(0, 0)
			};
			va.Buffer[1] = new PositionNormalTexture()
			{
				Pos = new Vector3(s, -s, s),
				Normal = new Vector3(0, 0, -1),
				TexCoord = new Vector2(1, 0)
			};
			va.Buffer[2] = new PositionNormalTexture()
			{
				Pos = new Vector3(s, s, s),
				Normal = new Vector3(0, 0, -1),
				TexCoord = new Vector2(1, 1)
			};
			va.Buffer[3] = new PositionNormalTexture()
			{
				Pos = new Vector3(-s, s, s),
				Normal = new Vector3(0, 0, -1),
				TexCoord = new Vector2(0, 1)
			};
			
			ib[0] = 0;
			ib[1] = 1;
			ib[2] = 2;
			ib[3] = 0;
			ib[4] = 2;
			ib[5] = 3;
			
			// Front
			va.Buffer[4] = new PositionNormalTexture()
			{
				Pos = new Vector3(-s, -s, -s),
				Normal = new Vector3(0, 0, -1),
				TexCoord = new Vector2(0, 0)
			};
			va.Buffer[5] = new PositionNormalTexture()
			{
				Pos = new Vector3(s, -s, -s),
				Normal = new Vector3(0, 0, -1),
				TexCoord = new Vector2(1, 0)
			};
			va.Buffer[6] = new PositionNormalTexture()
			{
				Pos = new Vector3(s, s, -s),
				Normal = new Vector3(0, 0, -1),
				TexCoord = new Vector2(1, 1)
			};
			va.Buffer[7] = new PositionNormalTexture()
			{
				Pos = new Vector3(-s, s, -s),
				Normal = new Vector3(0, 0, -1),
				TexCoord = new Vector2(0, 1)
			};
			
			ib[6] = 0 + 4;
			ib[7] = 2 + 4;
			ib[8] = 1 + 4;
			ib[9] = 0 + 4;
			ib[10] = 3 + 4;
			ib[11] = 2 + 4;
			
			
			
			va.Buffer.BufferData(BufferUsageHint.StaticDraw);
			ib.BufferData(BufferUsageHint.StaticDraw);
		}
		
		public void Draw()
		{
			va.MakeCurrent();
			ib.MakeCurrent();
			shader.MakeCurrent();
			shader.Projection.Value = Matrix4.CreatePerspectiveFieldOfView((float)Math.PI/3,1f, .1f, 1000f); //Scene.Owner.CurrentCamera.Projection;
			shader.View.Value = Matrix4.CreateTranslation(-3f, -3f, -3f);//Matrix4.Identity; //Matrix4.CreateTranslation(0,0, 32f);//Scene.Owner.CurrentCamera.AbsoluteTransform;
			shader.Model.Value = Matrix4.Identity;//Parent.AbsoluteTransform;
			shader.ModelView.Value = shader.Model.Value * shader.View.Value;
			var norm = shader.ModelView.Value;
			norm.Invert();
			norm.Transpose();
			shader.NormalMatrix.Value = norm;
			shader.ModelViewProjection.Value = shader.Model.Value * shader.View.Value * shader.Projection.Value;
			
			GL.DrawElements(BeginMode.Triangles, ib.Count,DrawElementsType.UnsignedShort, 0);
		}
	}
}

